package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.RefundSalesMapper;
import com.ruoyi.crm.domain.RefundSales;
import com.ruoyi.crm.service.IRefundSalesService;

/**
 * 销售退款单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-30
 */
@Service
public class RefundSalesServiceImpl implements IRefundSalesService
{
    @Autowired
    private RefundSalesMapper refundSalesMapper;

    /**
     * 查询销售退款单
     *
     * @param id 销售退款单主键
     * @return 销售退款单
     */
    @Override
    public RefundSales selectRefundSalesById(String id)
    {
        return refundSalesMapper.selectRefundSalesById(id);
    }

    /**
     * 查询销售退款单列表
     *
     * @param refundSales 销售退款单
     * @return 销售退款单
     */
    @Override
    public List<RefundSales> selectRefundSalesList(RefundSales refundSales)
    {
        return refundSalesMapper.selectRefundSalesList(refundSales);
    }

    /**
     * 新增销售退款单
     *
     * @param refundSales 销售退款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRefundSales(RefundSales refundSales)
    {
        int rows = refundSalesMapper.insertRefundSales(refundSales);
        insertCustomerProduct(refundSales);
        return rows;
    }

    /**
     * 修改销售退款单
     *
     * @param refundSales 销售退款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRefundSales(RefundSales refundSales)
    {
        refundSales.setUpdateTime(DateUtils.getNowDate());
        refundSalesMapper.deleteCustomerProductByCustomerName(refundSales.getId());
        insertCustomerProduct(refundSales);
        return refundSalesMapper.updateRefundSales(refundSales);
    }

    /**
     * 批量删除销售退款单
     *
     * @param ids 需要删除的销售退款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRefundSalesByIds(String[] ids)
    {
        return refundSalesMapper.deleteRefundSalesByIds(ids);
    }

    /**
     * 删除销售退款单信息
     *
     * @param id 销售退款单主键
     * @return 结果
     */
    @Override
    public int deleteRefundSalesById(String id)
    {
        refundSalesMapper.deleteCustomerProductByCustomerName(id);
        return refundSalesMapper.deleteRefundSalesById(id);
    }

    @Override
    public RefundSales getInfoByRefundNo(String refundNo) {
        return refundSalesMapper.getInfoByRefundNo(refundNo);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param refundSales 销售退款单对象
     */
    public void insertCustomerProduct(RefundSales refundSales)
    {
        List<CustomerProduct> customerProductList = refundSales.getCustomerProductList();
        String id = refundSales.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                refundSalesMapper.batchCustomerProduct(list);
            }
        }
    }
}